<?php
/**
 * Created by PhpStorm.
 * User: ycbwl
 * Date: 2018/10/23
 * Time: 11:30 PM
 */
namespace app\api\controller\v1;

use app\api\validate\Count;
use app\api\model\Product as ProductModel;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\ProductException;

class Product extends Base {

    /**
     * @url /product/recent
     * @return product models
     */
    public function getRecent($count = 15){
        (new Count())->goCheck();
        $result = ProductModel::getMostRecent($count);
        if($result->isEmpty()){
            throw new ProductException();
        }
        return $result;
    }


    public function getAllInCategory($id){
        (new IDMustBePositiveInt())->goCheck();
        $products = ProductModel::getProductsByCategoryID($id);
        if($products->isEmpty()){
            throw  new ProductException();
        }
        return $products;
    }

    public function getOne($id){
        (new IDMustBePositiveInt())->goCheck();
        $result = ProductModel::getProductDetail($id);
        if(!$result){
            throw new ProductException();
        }
        return $result;
    }
}